import pathToRegexp from 'path-to-regexp/index';
import {getLearnplan,getYLBM,getMaillist,postLearnplan,deleteLearnplan,putLearnplan,getLearnplanList} from 'services/app';
export default {
    namespace: 'Learnplan',
    state: {
        pages:1,
        record:{},
        gotoListData:{},
    },
    subscriptions: {
        setupHistory ({ dispatch, history }) {
            history.listen((location) => {
                const match = pathToRegexp('/home').exec(location.pathname);
                if(location.pathname == '/Learningplan_list'){
                    dispatch({type:'gotoList',payload:{id:localStorage.getItem('id')}});
                }
            });
        },
        setup ({ dispatch }) {},
    },
    effects: {
        //查看登录状态
        *getLearnplan ({ payload }, { call, put }){
            const {data,total} = yield call(getLearnplan, payload);
            yield put({ type: 'save', payload: {getLearnplanData:data,total:total,pages:payload.pageNumber}});
        },
        //获取添加数据
        *newpost ({ payload }, { call, put }){
            const data = yield call(getMaillist, {
                name:"",
                phone :"",
                pageNumber :0,
                pageSize :0,
            });
            const dataTow = yield call(getYLBM, payload);
            yield put({ type: 'save', payload: {getYLBMData:dataTow.data,getTXLData:data.data}});
        },
        //添加学习计划
        *postLearnplan ({ payload }, { call, put }){
            const data = yield call(postLearnplan,payload);
            if(data.code == 0){
                yield put({ type: 'save', payload: {pages:0,view:false}});
            }
        },
        //删除学习计划
        *deleteLearnplan  ({ payload }, { call, put }){
            const data = yield call(deleteLearnplan,payload);
            if(data.code == 0){
                yield put({ type: 'save', payload: {pages:0}});
            }
        },
        //修改学习计划
        *putLearnplan ({ payload }, { call, put }){
            const data = yield call(putLearnplan,payload);
            if(data.code == 0){
                yield put({ type: 'save', payload: {pages:0,view:false}});
            }
        },
        //详情
        *gotoList ({ payload }, { call, put }){
            const {data} = yield call(getLearnplanList,payload);
            yield put({ type: 'save', payload: {gotoListData:data[0]}});
        },
    },
    reducers: {
        save (state, { payload }) {
            return { ...state, ...payload };
        },
    },
};
